package com.wx.activitys.admin.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wx.activitys.base.utils.WebUtil;
import com.wx.activitys.drp.JsonResult;
import com.wx.activitys.drp.model.BUserInfo;
import com.wx.activitys.drp.service.BUserInfoService;

@Controller
@RequestMapping("/bUserInfo")
public class UserInfoController {

	@Autowired
	private BUserInfoService userInfoService;

	/**
	 * 认证审核列表
	 * 
	 * @param model
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(Model model, HttpServletRequest request) throws UnsupportedEncodingException {
		// String pathRoot =
		// request.getSession().getServletContext().getRealPath("");
		getList(request, model);
		return "admin/userInfoManage/list";
	}

	/**
	 * 个人注册列表
	 * 
	 * @param model
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "registerlist", method = RequestMethod.GET)
	public String registerlist(Model model, HttpServletRequest request) throws UnsupportedEncodingException {
		getList(request, model);
		return "admin/userInfoManage/registerlist";
	}

	private void getList(HttpServletRequest request, Model model) throws UnsupportedEncodingException {
		String openid = WebUtil.getString(request, "openid", "");
		String authInfo = WebUtil.getString(request, "authInfo", "");
		int page_index = WebUtil.getInt(request, "pageNo", 1);
		int pageStartRow = (page_index - 1) * 15;// 每页的起始数
		BUserInfo entity = new BUserInfo();
		if (!authInfo.equals("")) {
			entity.setAuthInfo(authInfo);
		}
		if (!openid.equals("")) {
			entity.setpId(openid);
			model.addAttribute("openid", openid);
		}
		entity.getMap().put("pageIndex", pageStartRow);
		entity.getMap().put("pageSize", 15);
		List<BUserInfo> platfromList = userInfoService.queryPageList(entity);
		int page_all = userInfoService.selectCount(entity);
		int pageSum = page_all % 15 == 0 ? page_all / 15 : (page_all / 15) + 1;
		model.addAttribute("List", platfromList);
		model.addAttribute("pageSum", pageSum);// 总页数
		model.addAttribute("totalSize", page_all);// 总页数
		model.addAttribute("pageNo", WebUtil.getInt(request, "pageNo", 1));// 当前显示页
	}

	@RequestMapping(value = "newPage.html", method = RequestMethod.POST)
	@ResponseBody
	public Object newPage(Model model, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<>();
		BUserInfo entity = new BUserInfo();
		int pageNo = WebUtil.getInt(request, "pageNo", 1);
		int attestationStatus = WebUtil.getInt(request, "attestationStatus", 10);
		String name = WebUtil.getString(request, "name", "");
		String phone = WebUtil.getString(request, "phone", "");
		String roleKey = WebUtil.getString(request, "roleName", "");
		String authInfo = WebUtil.getString(request, "authInfo", "");
		String startDate = WebUtil.getString(request, "startDate", "");
		String endDate = WebUtil.getString(request, "endDate", "");
		String authenticationType = WebUtil.getString(request, "authenticationType", "");
		String city = WebUtil.getString(request, "city", "");
		String pId = WebUtil.getString(request, "pId", "");
		int pageStartRow = (pageNo - 1) * 15;// 每页的起始数
		if (!name.equals("")) {
			entity.setName(name);
		}
		if (!roleKey.equals("")) {
			entity.setRoleKey(roleKey);
			;
		}
		if (!phone.equals("")) {
			entity.setPhone(phone);
		}
		if (!authInfo.equals("")) {
			entity.setAuthInfo(authInfo);
		}
		if (!authenticationType.equals("")) {
			entity.setAuthenticationType(authenticationType);
		}
		if (attestationStatus < 10) {
			entity.setAttestationStatus(attestationStatus);
		}
		if (!endDate.equals("") && !startDate.equals("")) {
			entity.getMap().put("startDate", startDate);
			entity.getMap().put("endDate", endDate);
		}
		if (!city.equals("")) {
			entity.setCity(city);
		}
		if(!pId.equals("")){
			if(pId.equals("1")){
				entity.getMap().put("IsNullpId", "1");
			}else if(pId.equals("2")){
				entity.getMap().put("IsNotNullpId", "2");
			}else{
				entity.equals(pId);
			}
		}
		entity.getMap().put("pageIndex", pageStartRow);
		entity.getMap().put("pageSize", 15);
		List<BUserInfo> platfromList = userInfoService.queryPageList(entity);
		Integer count = userInfoService.selectCount(entity);
		int pageSum = count % 15 == 0 ? count / 15 : (count / 15) + 1;
		map.put("platfromList", platfromList);
		map.put("count", count);
		map.put("pageSum", pageSum);
		map.put("pageNo", pageNo);
		return map;
	}

	/**
	 * 认证审核页面
	 * 
	 * @param model
	 * @param request
	 * @param openid
	 * @return
	 */
	@RequestMapping(value = "view", method = RequestMethod.GET)
	public String view(Model model, HttpServletRequest request, String openid) {
		BUserInfo bUserInfo = new BUserInfo();
		bUserInfo.setOpenid(openid);
		;
		BUserInfo entity = userInfoService.selectEntity(bUserInfo);
		model.addAttribute("entity", entity);
		model.addAttribute("authenticationType", entity.getAuthenticationType());
		return "admin/userInfoManage/view";
	}

	/**
	 * 认证申请审核
	 * 
	 * @param model
	 * @param state
	 * @param openid
	 * @return
	 */
	@RequestMapping(value = "saveInfo", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult saveInfo(Model model, String openid, String json, int attestationStatus) {

		BUserInfo entity = new BUserInfo();
		entity.setAuthInfo(json);
		entity.setOpenid(openid);
		entity.setAttestationStatus(attestationStatus);
		int result = userInfoService.update(entity);

		return JsonResult.success("1", result);
	}

	/**
	 * 经销商列表
	 * 
	 * @param model
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "memberList", method = RequestMethod.GET)
	public String memberList(Model model, HttpServletRequest request) throws UnsupportedEncodingException {
		getList(request, model);
		return "admin/userInfoManage/memberList";
	}

	/**
	 * 经销商修改页面
	 * 
	 * @param model
	 * @param request
	 * @param entity
	 * @return
	 */
	@RequestMapping(value = "update", method = RequestMethod.GET)
	public String update(Model model, HttpServletRequest request, String openid) {

		BUserInfo entity = new BUserInfo();
		entity.setOpenid(openid);
		entity = userInfoService.selectEntity(entity);
		model.addAttribute("entity", entity);
		return "admin/userInfoManage/form";
	}

	/**
	 * 经销商修改
	 * 
	 * @param model
	 * @param request
	 * @param entity
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public String save(Model model, BUserInfo entity) throws UnsupportedEncodingException {
		userInfoService.update(entity);

		HttpServletRequest request = null;
		getList(request, model);
		return "admin/userInfoManage/memberList";
	}

	/**
	 * 状态修改
	 * 
	 * @param model
	 * @param state
	 * @param openid
	 * @return
	 */
	@RequestMapping(value = "updateState", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult updateState(Model model, Integer state, String openid, String roleKey,
			HttpServletRequest request) {
		String gradeId = request.getParameter("gradeId");
		BUserInfo entity = new BUserInfo();
		entity.setState(state);
		entity.setOpenid(openid);
		entity.setRoleKey(roleKey);
		int result = userInfoService.updateState(entity, gradeId);
		if (result > 0) {
			return JsonResult.success("操作成功", result);
		} else {
			return JsonResult.success("操作失败", result);
		}

	}

	/**
	 * 经销商等级批量修改
	 * 
	 * @param model
	 * @param state
	 * @param openid
	 * @param roleKey
	 * @return
	 */
	@RequestMapping(value = "updateGrade", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult update(HttpServletRequest request, Model model, @RequestParam("openid[]") String[] ids,
			String roleKey) {
		int flag = userInfoService.batchupdate(roleKey, ids);
		if (flag > 0) {
			return JsonResult.success("操作成功", flag);
		} else {
			return JsonResult.success("操作失败", flag);
		}

	}

	/**
	 * 批量停用启用
	 * 
	 * @param model
	 * @param session
	 * @param request
	 * @param response
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public String deletePlan(HttpServletRequest request, Model model, @RequestParam("id[]") String[] ids, Integer state)
			throws Exception {
		BUserInfo entity = new BUserInfo();
		entity.getMap().put("ids", ids);
		entity.setState(state);
		userInfoService.update(entity);
		getList(request, model);
		return "admin/userInfoManage/memberList";
	}

	/**
	 * 批量修改经销商等级跳转
	 * 
	 * @param request
	 * @param model
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/grade", method = RequestMethod.GET)
	public String grade(HttpServletRequest request, Model model, @RequestParam("args") String[] ids) throws Exception {
		model.addAttribute("args", ids);
		return "admin/userInfoManage/grade";
	}

	/**
	 * 注册统计页
	 * 
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/statisticsList", method = RequestMethod.GET)
	public String statisticsGrade(HttpServletRequest request, Model model) throws Exception {
		BUserInfo entity = new BUserInfo();
		List<BUserInfo> countLists = userInfoService.queryTopTenList(entity);
		model.addAttribute("countLists", countLists);
		entity.getMap().put("pageIndex", 0);
		entity.getMap().put("pageSize", 10);
		List<BUserInfo> allList = userInfoService.queryPageRegistList(entity);
		model.addAttribute("allList", allList);
		Integer count = userInfoService.count(entity);
		int pageSum = count % 10 == 0 ? count / 10 : (count / 10) + 1;
		model.addAttribute("count", count);
		model.addAttribute("pageSum", pageSum);
		model.addAttribute("pageNo", 1);
		return "admin/statisticsManager/list";
	}
	
   /**
    * 注册统计页分页
    * @param model
    * @param request
    * @return
    */
	@RequestMapping(value = "newPage2.html", method = RequestMethod.POST)
	@ResponseBody
	public Object newPage2(Model model, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<>();
		BUserInfo entity = new BUserInfo();
		int pageNo = WebUtil.getInt(request, "pageNo", 1);
		String name = WebUtil.getString(request, "name", "");
		int pageStartRow = (pageNo - 1) * 10;// 每页的起始数
		if (!name.equals("")) {
			entity.setName(name);
		}
		entity.getMap().put("pageIndex", pageStartRow);
		entity.getMap().put("pageSize", 10);
		List<BUserInfo> platfromList = userInfoService.queryPageRegistList(entity);
		Integer count = userInfoService.count(entity);
		int pageSum = count % 10 == 0 ? count / 10 : (count / 10) + 1;
		map.put("platfromList", platfromList);
		map.put("count", count);
		map.put("pageSum", pageSum);
		map.put("pageNo", pageNo);
		return map;
	}

}
